package com.example.pinkcat.adapter;

import android.content.Context;
import android.content.Intent;
import android.mtp.MtpConstants;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.accessibility.AccessibilityManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.pinkcat.R;
import com.example.pinkcat.activity.Userpage;
import com.example.pinkcat.activity.VideoActivity;
import com.example.pinkcat.activity.api.getData.GetData;
import com.example.pinkcat.activity.pinkCatUser.userStaticMsg;
import com.example.pinkcat.api.getData.getData_waibu;
import com.example.pinkcat.api.entity.VideoInfoEntity;
import com.rishabhharit.roundedimageview.RoundedImageView;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.SimpleTimeZone;

import retrofit2.http.POST;

public class Dongtai_RecyclerViewAdapter extends RecyclerView.Adapter<Dongtai_RecyclerViewAdapter.ViewHolder> {

    private Context context;

    private Calendar mCalendar;

    private String video_pub_time;

    List<VideoInfoEntity> videoMsgArrayList=new ArrayList<>();
    public VideoInfoEntity vinfo;


    public void addData(List<VideoInfoEntity> videoEntities){
        this.videoMsgArrayList= videoEntities;
        notifyDataSetChanged();
    }

    public Dongtai_RecyclerViewAdapter(Context context) {
        this.context = context;
    }

    public class ViewHolder extends RecyclerView.ViewHolder{


        private RoundedImageView roundedImageView; //用户头像
        private TextView username;   //用户昵称
        private TextView tougaotime; //上传时间
        private TextView jianjie;    //视频简介
        private RoundedImageView jiemiam; //视频封面
        private TextView videoname;  //视频标题
        private TextView chatnum; //评论数量
        private TextView dianzannum; //点赞数量
        private View list_userpage; //动态首部
        public View chat; //评论总体
        public ImageView like; //点赞
        public RelativeLayout share;//分享


        public ViewHolder(@NonNull View itemView) {
            super(itemView);

            roundedImageView = itemView.findViewById(R.id.dongtai_tx);
            username = itemView.findViewById(R.id.dongtai_username);
            tougaotime = itemView.findViewById(R.id.dongtai_tougao_time);
            jianjie = itemView.findViewById(R.id.dongtai_jianjie);
            jiemiam = itemView.findViewById(R.id.dongtai_video_jiemian);
            videoname = itemView.findViewById(R.id.dongtai_video_name);
            chatnum = itemView.findViewById(R.id.chat_num);
            chat = itemView.findViewById(R.id.chat);
            dianzannum = itemView.findViewById(R.id.dianzan_num);
            list_userpage = itemView.findViewById(R.id.main_body_dongtai_1);
            like = itemView.findViewById(R.id.dianzan);
            share = itemView.findViewById(R.id.share);
        }
    }


    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view;
        view = LayoutInflater.from(context).inflate(R.layout.layout_dongtai_list,parent,false);
        return new Dongtai_RecyclerViewAdapter.ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        //Calendar 转String
        mCalendar = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dataSrt = sdf.format(mCalendar.getTime());

        Collections.sort(videoMsgArrayList, new Comparator<VideoInfoEntity>() {
            @Override
            public int compare(VideoInfoEntity o1, VideoInfoEntity o2) {

                return o2.getVideo_pub_time().compareTo(o1.getVideo_pub_time());
            }
        });

        if (videoMsgArrayList.size() == 0) {
            return;
        } else {
            Glide.with(context).load(Uri.parse(videoMsgArrayList.get(position).getHead_photo())).into(holder.roundedImageView);
            holder.username.setText(String.valueOf(videoMsgArrayList.get(position).getUser_name()));
            holder.tougaotime.setText(getTime(position));
            holder.jianjie.setText(String.valueOf(videoMsgArrayList.get(position).getVideo_info()));
            Glide.with(context).load(Uri.parse(videoMsgArrayList.get(position).getVideo_cover())).into(holder.jiemiam);
            holder.videoname.setText(String.valueOf(videoMsgArrayList.get(position).getVideo_name()));
            if (videoMsgArrayList.get(position).getLike_num() == null) {
                holder.dianzannum.setText("0");
            } else
                holder.dianzannum.setText(videoMsgArrayList.get(position).getLike_num());

            if (videoMsgArrayList.get(position).getTotalplay() == null)
                holder.chatnum.setText("0");
            else
                holder.chatnum.setText(videoMsgArrayList.get(position).getTotalplay());

            if (videoMsgArrayList.get(position).getComment_count() == null) {
                holder.chatnum.setText("0");
            } else {
                holder.chatnum.setText(String.valueOf(videoMsgArrayList.get(position).getComment_count()));
            }

            if (videoMsgArrayList.get(position).getLike() != null) {
                if (videoMsgArrayList.get(position).getLike().equals("0")||videoMsgArrayList.get(position).getLike().equals("-1")) {
                    holder.like.setSelected(false);
                } else if (videoMsgArrayList.get(position).getLike().equals("1")) {
                    holder.like.setSelected(true);
                }
            } else
                holder.like.setSelected(false);
        }


            holder.share.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(context, "感谢分享", Toast.LENGTH_SHORT).show();
                }
            });


            //个人页跳转
            holder.list_userpage.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context, Userpage.class);
                    intent.putExtra("up_id", videoMsgArrayList.get(position).getUser_id());
                    context.startActivity(intent);
                }
            });


            holder.jiemiam.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context, VideoActivity.class);
                    intent.putExtra("v_id", videoMsgArrayList.get(position).getVideo_id());
                    intent.putExtra("up_id", videoMsgArrayList.get(position).getUser_id());
                    intent.putExtra("v_link", videoMsgArrayList.get(position).getVideo_link());
                    intent.putExtra("v_labs", videoMsgArrayList.get(position).getVideo_lab());
                    context.startActivity(intent);
                }
            });

            holder.chat.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context, VideoActivity.class);
                    intent.putExtra("isCommentPage", true);
                    intent.putExtra("v_id", videoMsgArrayList.get(position).getVideo_id());
                    intent.putExtra("up_id", videoMsgArrayList.get(position).getUser_id());
                    intent.putExtra("v_link", videoMsgArrayList.get(position).getVideo_link());
                    intent.putExtra("v_labs", videoMsgArrayList.get(position).getVideo_lab());
                    context.startActivity(intent);
                }
            });

            //点赞
            holder.like.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (holder.like.isSelected()) {
                            Toast.makeText(context,"已经点赞过了", Toast.LENGTH_SHORT).show();
                            return;
                    }
                        if (getData_waibu.video_like__un(context, videoMsgArrayList.get(position).getVideo_id(), "like")) {
                            holder.like.setSelected(true);
                        }
                    }
            });

        }



    @Override
    public int getItemCount() {
        if (videoMsgArrayList.size()==0){
            return 0;
        }else {
        return videoMsgArrayList.size();}
    }


    //返回数据类型
    @Override
    public int getItemViewType(int position) {
        if (position==0){
            return 0;
        }
        else {
            return 1;
        }
    }


    private String getTime(int position) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        Date date = null;

        //从日历中获取默认时间(即当时时间)
        mCalendar = Calendar.getInstance();
        int month = mCalendar.get(Calendar.MONTH);
        int day = mCalendar.get(Calendar.DAY_OF_MONTH);
        int hour = mCalendar.get(Calendar.HOUR_OF_DAY);
        int mimute = mCalendar.get(Calendar.MINUTE);
        int year = mCalendar.get(Calendar.YEAR);


        //动态视频上传时间
        video_pub_time = String.valueOf(videoMsgArrayList.get(position).getVideo_pub_time());
        try {
            date =sdf.parse(video_pub_time);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        Calendar videotime = Calendar.getInstance();
        videotime.setTime(date);
        int pass_month = videotime.get(Calendar.MONTH);
        int pass_day = videotime.get(Calendar.DAY_OF_MONTH);
        int pass_hour = videotime.get(Calendar.HOUR_OF_DAY);
        int pass_minute = videotime.get(Calendar.MINUTE);
        int pass_year = videotime.get(Calendar.YEAR);

        //计算动态视频上传时间
        if (year - pass_year ==0){
        if (month -pass_month ==0){
            if (day-pass_day==0){
                if (hour-pass_hour==0){
                    if (mimute-pass_minute==0){
                        return "刚刚上传";
                    }else{
                        return (Math.abs(mimute - pass_minute) +"分钟前上传");
                    }
                }else {
                    return (Math.abs(hour-pass_hour) + "小时前上传");
                }
            } else {
                return (Math.abs(day-pass_day) + "天前上传");}
        }else return (Math.abs(month - pass_month)+"月前上传");
    }else return video_pub_time;
    }





    //string 转Calendar
    public long strToCalendar(String time){
        SimpleDateFormat sdf =  new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        Date date = null;

        try {
            date = sdf.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //获取当前时间
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.getTimeInMillis();
    }

    //时间转换为时间戳
    public static String dateToStamp(String s) throws ParseException {
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = simpleDateFormat.parse(s);
        long ts = date.getTime();
        res = String.valueOf(ts);
        return res;
    }
}
